import com.mit.web.business.proxy.PurchaseProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.DisplayConfiguration;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.FlexUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.purchase.POItemEditWindow;
import com.mit.web.vo.purchase.POItemVO;
import com.mit.web.vo.purchase.POVO;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.logging.ILogger;
import mx.rpc.events.ResultEvent;

[Bindable] private var _order:POVO = new POVO();
private var _proxy:PurchaseProxy = PurchaseProxy.getInstance();
private var _pm:IProgressMonitor;
private var _logger:ILogger;

private function datagridCreationCompleteHanlder(event:FlexEvent):void{
	var rsp:IInvokeResponder = _proxy.getPendingPurchaseItems();
	rsp.addResultListener(retrieveListHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function retrieveListHandler(event:ResultEvent):void{
	var lst:ArrayCollection = event.result as ArrayCollection;
	//有记录返回才更新，否则只是提示无记录存在
	if(lst != null && lst.length > 0){
		_order.itemList = lst;
	}else{
		MessageUtil.popNoRecordMessage(this);
		btn_save.enabled =false;
	}
}

private function doEdit(event:ListEvent):void{
	var item:POItemVO = _order.itemList[event.rowIndex] as POItemVO;
	var window:POItemEditWindow = new POItemEditWindow();
	window.item = item;
	window.addEventListener(PopWinCloseEvent.SAVED, purchasedHanlder);
	PopUpManager.addPopUp(window,this.owner,true);
	PopUpManager.centerPopUp(window);
}

private function purchasedHanlder(event:PopWinCloseEvent):void{
	dg.validateNow();	
}
private function doSave(event:MouseEvent):void{
	if(lst_buyer.selectedId == 0 ){
		showRequiredErrorTip(lst_buyer);
		return;
	}
	//传入后台以前，移除本次采购数量为0 的PO item
	var purchaseItems:ArrayCollection = new ArrayCollection();
	for each(var item:POItemVO in _order.itemList){
		if(item.qty > 0){
			purchaseItems.addItem(item);
		}
	}
	
	if(purchaseItems.length == 0 ){
		showVerticalMiddleErrorTipAt("请双击一条记录以输入本次采购的内容", dg);
		return;
	}
	_order.creationDate = dat_creationDate.selectedDate;
	_order.buyer = lst_buyer.selectedId;
	_order.buyerName = lst_buyer.selectedItem.label;
	
	var newPO:POVO = FlexUtil.deepClone(_order) as POVO;
	newPO.itemList = purchaseItems;
	
	var rsp:IInvokeResponder = _proxy.bulkCreatePO(newPO);
	rsp.addResultListener(createPOHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function createPOHandler(event:ResultEvent):void{
	var savedPOs:ArrayCollection = event.result as ArrayCollection;
	var savedPOCodes:ArrayCollection = new ArrayCollection();
	for each(var po:POVO in savedPOs){
		savedPOCodes.addItem(po.code);
	}
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.CREATED);	
	e.args = savedPOs;
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.saved", [savedPOCodes.source.join(",")]);
	this.dispatchEvent(e);
	closeWindow(this);
}